//
//  WBEmoticonPageCell.swift
//  weibo
//
//  Created by  郭航 on 16/7/5.
//  Copyright © 2016年 郭航. All rights reserved.
//

import UIKit

let addButtonClickNot = "addButtonClickNot"
let deleteButtonClickNot = "deleteButtonClickNot"

class WBEmoticonPageCell: UICollectionViewCell {
    
    var emoticons: [WBEmoticonModel]? {
        didSet {
            for button in buttons {
                button.hidden = true
            }
            
            for (i, emoticon) in (emoticons?.enumerate())! {
                let button = buttons[i]
                button.hidden = false
                button.emoticon = emoticon
                
                if emoticon.isEmoji == true {
                    let code = emoticon.code ?? ""
                    button.setTitle(code.emojiStr(), forState: .Normal)
                    button.setImage(nil, forState: .Normal)
                } else {
                    let image = UIImage(named: emoticon.pngName, inBundle: WBEmoticonsBundle.shareEmoticonsBundle.bundle, compatibleWithTraitCollection: nil)
                    button.setTitle(nil, forState: .Normal)
                    button.setImage(image, forState: .Normal)
                }
            }
        }
    }
    
    var index: NSIndexPath? {
        didSet {
//            let sec = index?.section ?? 0
//            let item = index?.item ?? 0
//            
//            textLabel.text = "\(sec),\(item)"
        }
    }
    
    private lazy var textLabel: UILabel = UILabel(textColor: UIColor.grayColor(), textAlignment: .Center, font: 15)
    
    var buttons: [WBEmoticonButton] = [WBEmoticonButton]()
    
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: #selector(WBEmoticonPageCell.deleteButtonClick), forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        contentView.addSubview(textLabel)
//        textLabel.snp_makeConstraints { (make) in
//            make.trailing.bottom.equalTo(contentView)
//        }
        
        buttons = addButton()
        
        contentView.addSubview(deleteButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let buttonW = (SCREENW - 10) / CGFloat(EmoticonsMaxRow)
        let buttonH = (frame.size.height - 20) / CGFloat(EmoticonsMaxCol)
        
        for (i, button) in buttons.enumerate() {
            let x = i % EmoticonsMaxRow
            let y = i / EmoticonsMaxRow
            
            button.frame = CGRect(x: CGFloat(x) * buttonW + 5, y: CGFloat(y) * buttonH, width: buttonW, height: buttonH)
        }
        
        deleteButton.frame = CGRect(x: SCREENW - 5 - buttonW, y: buttonH * 2, width: buttonW, height: buttonH)
    }
    
    private func addButton() -> [WBEmoticonButton] {
        var buttons = [WBEmoticonButton]()
        
        for _ in 0 ..< 20 {
            let button = WBEmoticonButton()
            
            button.addTarget(self, action: #selector(WBEmoticonPageCell.addButtonClick(_:)), forControlEvents: .TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
            
            contentView.addSubview(button)
            buttons.append(button)
        }
        
        return buttons
    }
    
    @objc private func addButtonClick(button: WBEmoticonButton) -> Void {
        NSNotificationCenter.defaultCenter().postNotificationName(addButtonClickNot, object: button.emoticon)
    }
    
    @objc private func deleteButtonClick() -> Void {
        NSNotificationCenter.defaultCenter().postNotificationName(deleteButtonClickNot, object: nil)
    }
    
}
